package com.xiaoxu.spzx.manager.service.impl;


import com.xiaoxu.common.exception.XiaoXuException;
import com.xiaoxu.spzx.manager.mapper.SysMenuMapper;
import com.xiaoxu.spzx.manager.mapper.SysRoleMenuMapper;
import com.xiaoxu.spzx.manager.service.SysMenuService;
import com.xiaoxu.spzx.manager.utils.MenuHelper;
import com.xiaoxu.spzx.model.entity.system.SysMenu;
import com.xiaoxu.spzx.model.entity.system.SysUser;
import com.xiaoxu.spzx.model.vo.common.ResultCodeEnum;
import com.xiaoxu.spzx.model.vo.system.SysMenuVo;
import com.xiaoxu.spzx.utils.AuthContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.LinkedList;
import java.util.List;

@Service
public class SysMenuServiceImpl implements SysMenuService {
    @Autowired
    private SysMenuMapper sysMenuMapper ;

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;
    @Override
    public List<SysMenu> findNodes() {

       List<SysMenu> sysMenuList = sysMenuMapper.selectAll() ;

        if (CollectionUtils.isEmpty(sysMenuList)) return null;

        List<SysMenu> treeList = MenuHelper.buildTree(sysMenuList); //构建树形数据

        return treeList;
    }

    @Override
    public void insertMenu(SysMenu sysMenu) {
        sysMenuMapper.insert(sysMenu);

        // 新添加一个菜单，那么此时就需要将该菜单所对应的父级菜单设置为半开
        updateSysRoleMenuIsHalf(sysMenu) ;
    }
    //菜单修改
    @Override
    public void updateMenu(SysMenu sysMenu) {
        sysMenuMapper.update(sysMenu);
    }
    //删除菜单管理
    @Override
    public void deleteMenu(Long id) {
        //根据菜单id判断是否子菜单?
        int count = sysMenuMapper.CountByParentById(id);
        if (count > 0) {
            throw new XiaoXuException(ResultCodeEnum.NODE_ERROR);
        }
        //等于0 直接删除
        sysMenuMapper.deleteById(id);
    }
    //查询用户可以操作的菜单
    @Override
    public List<SysMenuVo> findUserMenuList() {
        //获取当前登录的UserId
        SysUser sysUser = AuthContextUtil.get();

        Long userId = sysUser.getId();          // 获取当前登录用户的id
        //根据UserId查询出当前用户分配的菜单id
        List<SysMenu> sysMenuList = sysMenuMapper.selectListByUserId(userId);

        //构建树形数据
        List<SysMenu> sysMenuTreeList = MenuHelper.buildTree(sysMenuList);

        return this.buildMenus(sysMenuTreeList);
    }

    // 将List<SysMenu>对象转换成List<SysMenuVo>对象
    private List<SysMenuVo> buildMenus(List<SysMenu> menus) {

        List<SysMenuVo> sysMenuVoList = new LinkedList<SysMenuVo>();
        for (SysMenu sysMenu : menus) {
            SysMenuVo sysMenuVo = new SysMenuVo();
            sysMenuVo.setTitle(sysMenu.getTitle());
            sysMenuVo.setName(sysMenu.getComponent());
            List<SysMenu> children = sysMenu.getChildren();
            if (!CollectionUtils.isEmpty(children)) {
                sysMenuVo.setChildren(buildMenus(children));
            }
            sysMenuVoList.add(sysMenuVo);
        }
        return sysMenuVoList;
    }


    private void updateSysRoleMenuIsHalf(SysMenu sysMenu) {

        // 查询是否存在父节点
        SysMenu parentMenu = sysMenuMapper.selectById(sysMenu.getParentId());

        if (parentMenu != null) {

            // 将该id的菜单设置为半开
            sysRoleMenuMapper.updateSysRoleMenuIsHalf(parentMenu.getId());

            // 递归调用
            updateSysRoleMenuIsHalf(parentMenu);

        }

    }

}
